/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	"github.com/astaxie/beego"

	"fmt"

	"time"
	//"WebAdminSys/libs"
	"WebAdminSys/models"
)

type UserGivegiftController struct {
	BaseController
}

func (self *UserGivegiftController) List() {
	fmt.Println("==================UserGivegiftController List========================");
	self.Data["pageTitle"] = "礼物赠送记录"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.display()
}
func (self *UserGivegiftController) Table() {
	
	returnRecord := self.GetString("returnRecord");
	if returnRecord == "" {
		self.giftGiveRecord() 
	}else{
		self.giftReturnRecord()
	}
	
}

func (self *UserGivegiftController) giftGiveRecord() {
	fmt.Println("==================UserGivegiftController giftGiveRecord========================")
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}
	self.pageSize = limit
	
	userID,err := self.GetInt64("userID"); 
	selectGiveType,err:= self.GetInt("selectGiveType"); //1 2赠送则 接收者
	date1:= self.GetString("date1");
	date2 := self.GetString("date2");
	selectGetType,err := self.GetInt("selectGetType"); //1 2 领取 未领取
	fmt.Println(userID,selectGiveType,date1,date2,selectGetType,page,limit);
	
	
	//time1,err := time.ParseInLocation("2006-02-01",date1,time.Local);
	//time2,err := time.ParseInLocation("2006-02-01",date2,time.Local);

		//接收者 为领取 忽略时间范围
		if selectGiveType == 2 && selectGetType ==2 {
			date1 = "";
			date2 = "";
		}
	
	
	//查询条件
	filters := make([]interface{}, 0)
	
	// selectGiveType 0 所有 1 赠送者 2 接收者
	if userID != 0 {
		if selectGiveType == 1 {
			filters = append(filters, "GiveUserID",userID)
		}else if selectGiveType == 2{
			filters = append(filters, "ReceiveUserID",userID)
		}
	}
	
	//selectGetType 0 所有 1 已领取 2 未领取 3 退回
	if selectGetType == 1 {
		filters = append(filters, "Status",1) //'礼物状态 0未领取 1已领取 2退回',
	}else if selectGetType == 2 {
		filters = append(filters, "Status",0) //'礼物状态 0未领取 1已领取 2退回',
	}else if selectGetType == 3 {
		filters = append(filters, "Status",2) //'礼物状态 0未领取 1已领取 2退回',
	}


	if date1 == "" && date2 == "" {
		
	}else{
				if date1 == "" {
					date1 = "2006-01-02 15:04:05"
				}
			
				if date2 == "" {
					date2 = "2019-01-02 15:04:05"
				}
				//filters = append(filters, "operation_time","2017-11-07 15:00:20","2017-11-07 17:00:20")
	}
	fmt.Println(filters);

	//获取本地location  
			//toBeCharge := "2015-01-01 00:00:00"                             //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写  
	timeLayout := "2006-01-02 15:04:05"                             //转化所需模板  
	loc, _ := time.LoadLocation("Local")                            //重要：获取时区  
	theTime1, _ := time.ParseInLocation(timeLayout, date1, loc)    //使用模板在对应时区转化为time.time类型  
	time1 := theTime1.Unix()                                             //转化为时间戳 类型是int64  
	
	theTime2, _ := time.ParseInLocation(timeLayout, date2, loc)    //使用模板在对应时区转化为time.time类型  
	time2 := theTime2.Unix()    
	if date1 == "" {time1 = 0}
	if date2 == "" {time2 = 0}

	fmt.Println("===>>时间转换\n",theTime1,theTime2,time1,time2)                                            //打印输出theTime 2015-01-01 15:15:00 +0800 CST  

	//result,count := models.GMAddRecordGetListSQL("SELECT id FROM GMAddRecord  LIMIT 1,1WHERE operation_time >=\"2017-11-00 12:40:05\" and operation_time<=\"2017-11-08 12:12:10\")
	result, count := models.GiveGiftRecordGetList(page, self.pageSize,time1,time2,selectGiveType, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["useridgive"] = v.GiveUserID
		row["nicknamegive"] = v.GiveNickName
		row["useridget"] = v.ReceiveUserID
		row["nicknameget"] = v.ReceiveNickName
		row["giftName"] = v.GiftName
		row["amount"] = v.Gold
		row["giftId"] = v.GiveGiftID
		
		row["timegive"] = beego.Date(time.Unix(v.GiveTime, 0),"Y-m-d H:i:s" )

		fmt.Println(" ")
		var str string;
		if v.Status == 0 {
			str = "0未领取"
		}else if v.Status == 1{
			str = "1已领取"
			tm := time.Unix(v.ReceiveTime, 0);
			//timestr := fmt.Sprintf("%04d-%02d-%02d 00:00:00", tm.Year(),tm.Month(),tm.Day())
			str = str + tm.Format(";2006-01-02 15:04:05"); //timestr //  beego.Date(tm,"Y-m-d H:i:s" )//

		}else if v.Status == 2{
			str ="2已退回"
			str = str + v.BackTime;
		}
		if v.ReceiveTime == 0 {
			
		}
		row["timeget"] =  str;
		row["operation"] = v.Status
	

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}
// 礼物退回 操作
func (self *UserGivegiftController) GiftReturn() {
	fmt.Println("==================UserGivegiftController giftReturn========================");

	userID,_ := self.GetInt64("userID"); 
	giftID,_ := self.GetInt64("giftID"); 
	gold ,_:= self.GetInt64("gold")
	
	va := models.UserCtrlInfoGetInsert();
	va.USERID = self.userId ;
	va.GiveGiftID = giftID;
	va.NTYPE = 4;	//4 礼物找回
	va.MailID = giftID;
	va.DESGAMEID = userID;
	va.GOLD = gold;
	n,err := models.UserCtrlInfoInsertValue(&va);
	fmt.Println(n,err);

	if err != nil {
		self.ajaxMsg(n,1)
	}else{
		self.ajaxMsg(n,0)
	}
	
}



// 礼物退回记录查询
func (self *UserGivegiftController) giftReturnRecord() {
	fmt.Println("==================UserGivegiftController giftReturnRecord========================")
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}
	self.pageSize = limit
	
	userID,err := self.GetInt64("userID"); 
	userID2,err := self.GetInt64("userID2"); 
	selectGiveType,err:= self.GetInt("selectGiveType"); //1 2赠送则 接收者
	date1:= self.GetString("date1");
	date2 := self.GetString("date2");
	selectGetType,err := self.GetInt("selectGetType"); //1 2 领取 未领取
	fmt.Println(userID,userID,selectGiveType,date1,date2,selectGetType,page,limit);
	
	
	//time1,err := time.ParseInLocation("2006-02-01",date1,time.Local);
	//time2,err := time.ParseInLocation("2006-02-01",date2,time.Local);

		//接收者 为领取 忽略时间范围
		if selectGiveType == 2 && selectGetType ==2 {
			date1 = "";
			date2 = "";
		}
	
	
	//查询条件
	filters := make([]interface{}, 0)
	
	// selectGiveType 0 所有 1 赠送者 2 接收者
	if userID != 0 && userID2 != 0 {

			filters = append(filters, "GiveUserID",userID)
			filters = append(filters, "ReceiveUserID",userID2)
		
	}
	
	//selectGetType 0 所有 1 已领取 2 未领取 3 退回
	filters = append(filters, "Status",2) //'礼物状态 0未领取 1已领取 2退回',
	


	if date1 == "" && date2 == "" {
		
	}else{
				if date1 == "" {
					date1 = "2006-01-02 15:04:05"
				}
			
				if date2 == "" {
					date2 = "2019-01-02 15:04:05"
				}
				//filters = append(filters, "operation_time","2017-11-07 15:00:20","2017-11-07 17:00:20")
	}
	fmt.Println(filters);

	//获取本地location  
			//toBeCharge := "2015-01-01 00:00:00"                             //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写  
	timeLayout := "2006-01-02 15:04:05"                             //转化所需模板  
	loc, _ := time.LoadLocation("Local")                            //重要：获取时区  
	theTime1, _ := time.ParseInLocation(timeLayout, date1, loc)    //使用模板在对应时区转化为time.time类型  
	time1 := theTime1.Unix()                                             //转化为时间戳 类型是int64  
	
	theTime2, _ := time.ParseInLocation(timeLayout, date2, loc)    //使用模板在对应时区转化为time.time类型  
	time2 := theTime2.Unix()    
	if date1 == "" {time1 = 0}
	if date2 == "" {time2 = 0}

	fmt.Println("===>>时间转换\n",theTime1,theTime2,time1,time2)                                            //打印输出theTime 2015-01-01 15:15:00 +0800 CST  

	//result,count := models.GMAddRecordGetListSQL("SELECT id FROM GMAddRecord  LIMIT 1,1WHERE operation_time >=\"2017-11-00 12:40:05\" and operation_time<=\"2017-11-08 12:12:10\")
	result, count := models.GiveGiftRecordGetList(page, self.pageSize,time1,time2,selectGiveType, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["useridgive"] = v.GiveUserID
		row["nicknamegive"] = v.GiveNickName
		row["useridget"] = v.ReceiveUserID
		row["nicknameget"] = v.ReceiveNickName
		row["giftName"] = v.GiftName
		row["amount"] = v.Gold	
		row["timegive"] = beego.Date(time.Unix(v.GiveTime, 0),"Y-m-d H:i:s" )
		row["timeget"] =  v.BackTime;
		row["operation"] = v.Gid
		row["giftMainID"] = v.GiveGiftID

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}
